package com.leech.lambda;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

//函数式接口,可以用lambda,接口中只有一个未实现的方法，这个接口就是函数式接口
interface MyInterface {
    int sum(int i, int j);
}

@FunctionalInterface
interface MyNoargsInterface {
    int noargs();
}

interface MyOneargsInterface {
    int oneargs(int x);
}

class MyInterfaceImpl implements MyInterface {
    @Override
    public int sum(int i, int j) {
        return i+j;
    }
}

public class Lambda {


    public static void main(String[] args) {
        // 有入参无出参
        BiConsumer<String,String> consumer = (a,b) -> {
            System.out.println("a="+a+";b="+b);
        };
        consumer.accept("hello", "world");

        // 有入参有出参
        Function<String, String> function = x -> {
            return x;
        };
        String helloWorld = function.apply("hello world");
        System.out.println(helloWorld);

        BiFunction<String,Integer,Long> biFunction = (x,y) -> {
            long xLong = Long.parseLong(x);
            long yLong = y.longValue();

            return xLong + yLong;
        };
        Long apply = biFunction.apply("34", 56);
        System.out.println(apply);


        // 无入参无出参
        Runnable runnable = () -> System.out.println("runnable");
        new Thread(runnable).start();

        // 无入参有出参
        Supplier<String> supplier = () -> UUID.randomUUID().toString();
        String uuid = supplier.get();
        System.out.println(uuid);

    }




    public static void methodB(String[] args) {
        var list = new ArrayList<String>();
        list.add("Alex");
        list.add("Bryant");
        list.add("Curry");
        list.add("Davis");

        /*Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        System.out.println(list);*/

        Collections.sort(list, (o1,o2) -> o1.compareTo(o2));
        Collections.sort(list, String::compareTo);
        System.out.println(list);

        new Thread(() -> System.out.println("hello")).start();
        // 以后调用某个方法传入参数，这个参数实例是一个接口对象，且只定义了一个方法，就直接用lambda简化
    }


    public static void methodA(String[] args) {
        MyInterface myInterface = new MyInterfaceImpl();
        System.out.println(myInterface.sum(1, 2));

        MyInterface myInterface1 = new MyInterface() {
            @Override
            public int sum(int i, int j) {
                return i*i + j*j;
            }
        };
        System.out.println(myInterface1.sum(2,3));

        //lambda
        MyInterface myInterface2 = (int i, int j) -> {
            return i*i + j*j;
        };
        System.out.println(myInterface2.sum(2,3));

        //简化写法
        MyInterface myInterface3 = (x, y) -> {
            return x*x + y*y;
        };
        System.out.println(myInterface3.sum(2,3));

        //无参
        MyNoargsInterface myNoargsInterface = () -> {
            return 2;
        };
        System.out.println(myNoargsInterface.noargs());

        // 一个参数
        MyOneargsInterface myOneargsInterface = x -> {
            return x*x;
        };
        System.out.println(myOneargsInterface.oneargs(8));

        MyOneargsInterface myOneargsInterface2 = x -> x*x;
        System.out.println(myOneargsInterface2.oneargs(8));
    }
}
